package com.fubang.tabhostdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.fubang.tabhostdemo.base.BaseFragment;
import com.fubang.tabhostdemo.base.CommonPagerAdapter;
import com.fubang.tabhostdemo.base.ControllerSlideViewPager;
import com.fubang.tabhostdemo.fragment.AlarmFragment;
import com.fubang.tabhostdemo.fragment.HomeFragment;
import com.fubang.tabhostdemo.fragment.MineFragment;
import com.fubang.tabhostdemo.fragment.SignFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    ControllerSlideViewPager mPager;
    RadioGroup mRg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPager = (ControllerSlideViewPager) findViewById(R.id.home_pager);
        mRg = (RadioGroup) findViewById(R.id.home_rg);
        initView();
        initData();
    }

    protected void initView() {
        mPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        mRg.setOnCheckedChangeListener(this);
    }

    protected void initData() {
        List<BaseFragment> fragments = new ArrayList<>();
        HomeFragment homeFragment = new HomeFragment();
        AlarmFragment alarmFragment = new AlarmFragment();
        SignFragment signFragment = new SignFragment();
        MineFragment mineFragment = new MineFragment();
        fragments.add(homeFragment);
        fragments.add(alarmFragment);
        fragments.add(signFragment);
        fragments.add(mineFragment);
        CommonPagerAdapter commonPagerAdapter = new CommonPagerAdapter(getSupportFragmentManager(), fragments);
        if (mPager != null) {
            mPager.setAdapter(commonPagerAdapter);
            mPager.setOffscreenPageLimit(4);
        }

    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.home_home:
                mPager.setCurrentItem(0);
                break;
            case R.id.home_alarm:
                mPager.setCurrentItem(1);
                break;
            case R.id.home_sign:
                mPager.setCurrentItem(3);
                break;
            case R.id.home_mine:
                mPager.setCurrentItem(4);
                break;
            default:
                break;
        }

    }
}
